//
//  HSCommentKeyboardView.m
//  HolyShit
//
//  Created by Robin on 2017/4/5.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSCommentKeyboardView.h"

const CGFloat OneRowHeight = 35;
const CGFloat TwoRowHeight = 46;
const NSUInteger MaxLength = 50;
const CGFloat KeyboardHeight = 216;

@interface HSCommentKeyboardView ()

@property (nonatomic, strong) UIView *contentView;
//@property (nonatomic, strong) UILabel *placeholderLb;
@property (nonatomic, assign) CGFloat initTop;
//@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIView *areaView;
@property (nonatomic, strong) UIButton *emojiBtn;
@property (nonatomic, strong) UILabel *tipLabel;


@end

@implementation HSCommentKeyboardView

-(instancetype)init
{
    if(self = [super init]){
        [self commonInit];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark - method
- (void)commonInit
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidDisappear:) name:UIKeyboardDidHideNotification object:nil];
    
    self.width = ScreenWidth;
    self.height = TwoRowHeight + 20;
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.tipLabel];
    [self.contentView addSubview:self.sendBtn];
    [self.contentView addSubview:self.areaView];
}

- (void)calculateTextViewHeight
{
    NSUInteger textLength = self.textView.text.length;
    
    self.placeholderLb.hidden = textLength > 0;
    CGFloat viewHeight = self.textView.contentSize.height > OneRowHeight ? TwoRowHeight : OneRowHeight;
    self.contentView.frame = CGRectMake(0, (TwoRowHeight - viewHeight), self.width, 20 + viewHeight);
    self.areaView.frame = CGRectMake(self.areaView.left, self.contentView.height - viewHeight - 10, self.areaView.width, viewHeight);
    self.textView.frame = CGRectMake(self.textView.left, 0, self.textView.width, viewHeight);
    self.emojiBtn.centerY = self.textView.centerY;
    
    BOOL isShowTip = textLength > MaxLength;
    if(isShowTip){
        self.tipLabel.text = [NSString stringWithFormat:@"-%@", @(textLength - MaxLength)];
        self.sendBtn.top = self.tipLabel.bottom - 2;
    }
    else{
        self.tipLabel.text = @"";
        self.sendBtn.centerY = self.contentView.height / 2.;
    }
    self.sendBtn.enabled = textLength > 0 && !isShowTip;
}

#pragma mark - getter & setter
- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, TwoRowHeight - OneRowHeight, self.width, OneRowHeight + 20)];
        _contentView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    }
    return _contentView;
}

- (UILabel *)placeholderLb
{
    if (!_placeholderLb) {
        _placeholderLb = [[UILabel alloc]initWithFrame:CGRectMake(3, 0, self.textView.width - 3, self.textView.height)];
        _placeholderLb.font = MYFont(Font28);
        _placeholderLb.textColor = HEXCOLOR(HSColorGray);
        _placeholderLb.text = @"一起来讨论吧";
    }
    return _placeholderLb;
}

- (UITextView *)textView
{
    if (!_textView) {
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, self.areaView.width - 35, self.areaView.height)];
        _textView.backgroundColor = CLEAR_COLOR;
        _textView.layer.cornerRadius = 5;
        _textView.font = MYFont(Font28);
        _textView.textColor = HEXCOLOR(HSColorBlack);
        _textView.showsVerticalScrollIndicator = NO;
        _textView.autocorrectionType = UITextAutocorrectionTypeNo;
        [_textView addSubview:self.placeholderLb];
        @weakify(self);
        [[_textView rac_textSignal]subscribeNext:^(id x) {
            @strongify(self);
            [self calculateTextViewHeight];
            self.placeholderLb.hidden = self.textView.text.length > 0;
        }];
    }
    return _textView;
}

- (UILabel *)tipLabel
{
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.width - 60, 13, 64, 13)];
        _tipLabel.font = MYFont(Font26);
        _tipLabel.textColor = HEXCOLOR(HSColorRed);
        _tipLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _tipLabel;
}

- (UIButton *)sendBtn
{
    if (!_sendBtn) {
        _sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.width - 64, 10, 64, OneRowHeight)];
        _sendBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        _sendBtn.backgroundColor = CLEAR_COLOR;
        _sendBtn.titleLabel.font = MYFont(Font32);
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [_sendBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        [_sendBtn setTitleColor:HEXCOLOR(HSColorGray) forState:UIControlStateDisabled];
        _sendBtn.enabled = NO;
    }
    return _sendBtn;
}

- (UIView *)areaView
{
    if (!_areaView) {
        _areaView = [[UIView alloc]initWithFrame:CGRectMake(10, self.sendBtn.top, self.sendBtn.left, OneRowHeight)];
        _areaView.backgroundColor = HEXCOLOR(HSColorBgLight);
        _areaView.layer.cornerRadius = 5;
        [_areaView addSubview:self.emojiBtn];
        [_areaView addSubview:self.textView];
    }
    return _areaView;
}

#pragma mark - keyboard
-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo//计算键盘的高度
{
    CGRect keyboardEndingUncorrectedFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    CGRect keyboardEndingFrame = [self convertRect:keyboardEndingUncorrectedFrame fromView:nil];
    return keyboardEndingFrame.size.height;
}

-(void)keyboardWillAppear:(NSNotification *)notification
{
    CGRect currentFrame = CGRectMake(0, ScreenHeight, self.width, self.height);
    CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
    currentFrame.origin.y = currentFrame.origin.y - change - self.height - NavBarHeight;
    self.frame = currentFrame;
}

-(void)keyboardWillDisappear:(NSNotification *)notification
{
    CGRect currentFrame = self.frame;
    CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
    currentFrame.origin.y = currentFrame.origin.y + change + self.height;
    self.frame = currentFrame;
}

-(void)keyboardDidDisappear:(NSNotification *)notification
{
    //低端机例如5c不靠谱，这里多执行一次，保证显示正常
    [self calculateTextViewHeight];
    if(self.keyboardDidHide){
        self.keyboardDidHide();
    }
    if (self.hideHandle) {
        self.hideHandle();
        [self setHideHandle:nil];
    }
}

@end
